package com.example.azheng.rxjavamvpdemo.ui.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.azheng.rxjavamvpdemo.R;
import com.example.azheng.rxjavamvpdemo.base.BaseMvpFragment;
import com.example.azheng.rxjavamvpdemo.bean.BaseArrayBean;
import com.example.azheng.rxjavamvpdemo.bean.banner.Banner;
import com.example.azheng.rxjavamvpdemo.bean.notice.Notice;
import com.example.azheng.rxjavamvpdemo.mvp.contract.HomeContract;
import com.example.azheng.rxjavamvpdemo.mvp.presenter.HomePresenter;
import com.example.azheng.rxjavamvpdemo.ui.acitivity.NoticeDetailActivity;
import com.example.azheng.rxjavamvpdemo.ui.acitivity.ServiceActivity;
import com.example.azheng.rxjavamvpdemo.util.GlideImageLoader;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Time:2020/5/19
 * Author:ypf
 * Description:
 */
public class HomeFragment extends BaseMvpFragment<HomePresenter> implements HomeContract.View {


    @BindView(R.id.tvNotice)
    TextView tvNotice;
    @BindView(R.id.banner)
    com.youth.banner.Banner banner;
    @BindView(R.id.tvServer)
    TextView tvServer;
    @BindView(R.id.v_notice)
    View vNotice;
    @BindView(R.id.iv_notice)
    ImageView ivNotice;
    @BindView(R.id.tv_notice_key)
    TextView tvNoticeKey;
    @BindView(R.id.imageView8)
    ImageView imageView8;
    @BindView(R.id.iv_index_gaming_right)
    ImageView ivIndexGamingRight;
    @BindView(R.id.textView38)
    TextView textView38;
    @BindView(R.id.textView39)
    TextView textView39;
    @BindView(R.id.textView40)
    TextView textView40;
    @BindView(R.id.textView42)
    TextView textView42;
    @BindView(R.id.textView41)
    TextView textView41;
    @BindView(R.id.imageView10)
    ImageView imageView10;
    @BindView(R.id.iv_index_gaming_right_2)
    ImageView ivIndexGamingRight2;
    @BindView(R.id.textView43)
    TextView textView43;
    @BindView(R.id.textView44)
    TextView textView44;
    @BindView(R.id.textView45)
    TextView textView45;
    @BindView(R.id.textView46)
    TextView textView46;
    @BindView(R.id.textView47)
    TextView textView47;
    @BindView(R.id.imageView14)
    ImageView imageView14;
    @BindView(R.id.iv_index_gaming_right_3)
    ImageView ivIndexGamingRight3;
    @BindView(R.id.textView49)
    TextView textView49;
    @BindView(R.id.textView50)
    TextView textView50;
    @BindView(R.id.textView51)
    TextView textView51;
    @BindView(R.id.textView52)
    TextView textView52;
    @BindView(R.id.textView53)
    TextView textView53;
    Unbinder unbinder;

    /**
     * 通知栏id
     */
    private int mNoticeDetailId = -1;

    @Override
    protected void initView(View view) {
        mPresenter = new HomePresenter();
        mPresenter.attachView(this);
        mPresenter.notice();
        mPresenter.banner();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void onNoticeSuccess(BaseArrayBean<Notice> bean) {

        if (null == bean) {
            return;
        }

        List<Notice> noticeList = bean.getData();

        if (0 == noticeList.size()) {
            return;
        }

        String title = noticeList.get(0).getTitle();
        mNoticeDetailId = noticeList.get(0).getId();
        if (!TextUtils.isEmpty(title)) {
            tvNotice.setText(title);
        }

    }

    @Override
    public void onBannerSuccess(BaseArrayBean<Banner> bean) {

        if (null == bean) {
            return;
        }

        final List<Banner> bannerList = bean.getData();

        if (0 == bannerList.size()) {
            return;
        }

        List<String> imageList = new ArrayList<>();

        for (int i = 0; i < bannerList.size(); i++) {
            imageList.add(bannerList.get(i).getImage());
        }

        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(imageList);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Intent intent = new Intent(getActivity(), NoticeDetailActivity.class);
                intent.putExtra("id", bannerList.get(position).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {
        Toast.makeText(getActivity(), "请求失败", Toast.LENGTH_SHORT).show();
    }

    @OnClick({R.id.tvServer, R.id.v_notice, R.id.iv_index_gaming_right, R.id.iv_index_gaming_right_2, R.id.iv_index_gaming_right_3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvServer:
                Intent serverIntent = new Intent(getActivity(), ServiceActivity.class);
                startActivity(serverIntent);
                break;
            case R.id.v_notice:
                if (-1 == mNoticeDetailId) {
                    return;
                }
                Intent intent = new Intent(getActivity(), NoticeDetailActivity.class);
                intent.putExtra("id", mNoticeDetailId);
                startActivity(intent);
                break;
            case R.id.iv_index_gaming_right:
                break;
            case R.id.iv_index_gaming_right_2:
                break;
            case R.id.iv_index_gaming_right_3:
                break;
        }
    }
}
